import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
export const columns: BasicColumn[] = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 60,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '名称',
    dataIndex: 'name',
    width: 100,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '类型',
    dataIndex: 'typeDesc',
    width: 50,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '规则',
    dataIndex: 'template',
    width: 120,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '当前值',
    dataIndex: 'curValue',
    width: 300,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '补齐位数',
    dataIndex: 'length',
    width: 100,
    ellipsis: true,
    resizable: true,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'by',
    label: '',
    component: 'Select',
    required: true,
    colProps: { span: 2 },
    defaultValue: 'name',
    componentProps: {
      options: [{ value: 'name', label: '名称' }],
    },
  },
  {
    field: 'what',
    label: '',
    component: 'Input',
    colProps: { span: 4 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'name',
    label: '编号名称',
    required: true,
    component: 'Input',
    colProps: { span: 24 },
  },
  {
    field: 'curValue',
    label: '当前值',
    component: 'Input',
    colProps: { span: 24 },
    componentProps: {
      readonly: false,
    },
  },
  {
    field: 'title3',
    label: '规则设置',
    component: 'Divider',
    colProps: { span: 24 },
  },
  {
    field: 'prefixValue',
    label: '前缀',
    component: 'Input',
    colProps: { span: 24 },
    componentProps: ({ formModel }) => {
      return {
        onChange: (e) => {
          console.log('value', e, formModel);
          formModel['prefixValue'] = e.target.value;
          formModel['template'] = resetTemplate(formModel);
        },
      };
    },
  },
  {
    field: 'dept',
    label: '部门',
    component: 'Checkbox',
    colProps: { span: 24 },
    componentProps: ({ formModel }) => {
      return {
        onChange: (e) => {
          console.log('value', e, formModel);
          formModel['dept'] = e.target.checked;
          formModel['template'] = resetTemplate(formModel);
        },
      };
    },
  },
  {
    field: 'yearNum',
    label: '年号',
    component: 'RadioGroup',
    colProps: { span: 24 },
    componentProps: ({ formModel }) => {
      return {
        options: [
          {
            label: '无',
            value: '',
          },
          {
            label: '年',
            value: '{date:yyyy}',
          },
          {
            label: '年月',
            value: '{date:yyyy-MM}',
          },
          {
            label: '年月日',
            value: '{date:yyyy-MM-dd}',
          },
        ],

        onChange: (e) => {
          console.log('value', formModel);
          formModel['yearNum'] = e.target.value;
          formModel['template'] = resetTemplate(formModel);
        },
      };
    },
  },
  {
    field: 'yearNumReset',
    label: '数值重置',
    component: 'RadioGroup',
    colProps: { span: 24 },
    componentProps: ({ formModel }) => {
      return {
        options: [
          {
            label: '不重置',
            value: '{num}',
          },
          {
            label: '按年',
            value: '{num:year}',
          },
          {
            label: '按月',
            value: '{num:month}',
          },
          {
            label: '按日',
            value: '{num:day}',
          },
        ],
        onChange: (e) => {
          console.log('value', formModel);
          formModel['yearNumReset'] = e.target.value;
          formModel['template'] = resetTemplate(formModel);
        },
      };
    },
  },
  {
    field: 'template',
    label: '规则',
    component: 'Input',
    defaultValue: '{num}',
    colProps: { span: 24 },
    componentProps: {
      readonly: true,
    },
  },
  {
    field: 'length',
    label: '补齐位数',
    component: 'Input',
    helpMessage: '数字长度位数，不足将在左侧补0，置为0表示不需要补位',
    colProps: { span: 24 },
  },
  {
    field: 'itemSeparator',
    label: '分隔符',
    component: 'Select',
    colProps: { span: 24 },
    componentProps: {
      options: [
        {
          label: '无',
          value: '',
        },
        {
          label: '减号',
          value: '-',
        },
        {
          label: '下划线',
          value: '_',
        },
        {
          label: '竖线',
          value: '|',
        },
      ],
    },
  },
  {
    field: 'yearDigit',
    label: '年份位数',
    component: 'Select',
    colProps: { span: 24 },
    componentProps: {
      options: [
        {
          label: '4',
          value: '4',
        },
        {
          label: '2',
          value: '2',
        },
      ],
    },
  },
];

export const resetTemplate = (model) => {
  const prefix = model['prefixValue'];
  const dept = model['dept'];
  const yearNum = model['yearNum'];
  const yearNumReset = model['yearNumReset'];
  let templateValue = '';

  if (prefix) {
    templateValue += prefix;
  }
  if (dept) {
    templateValue += '{dept}-';
  }

  if (yearNum) {
    templateValue += yearNum + '-';
  }
  if (yearNumReset) {
    templateValue += yearNumReset;
  }
  return templateValue;
};
